#include "patientprofilewidget.h"
#include "ui_patientprofilewidget.h"
#include "../../network/networkclient.h"
#include <QMessageBox>

PatientProfileWidget::PatientProfileWidget(const Patient& patient, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PatientProfileWidget),
    m_patient(patient)
{
    ui->setupUi(this);
    
    // 连接网络信号
    connect(&NetworkClient::instance(), &NetworkClient::messageReceived,
            this, &PatientProfileWidget::onMessageReceived);
    
    // 加载患者信息
    loadPatientInfo();
}

PatientProfileWidget::~PatientProfileWidget() {
    delete ui;
}

void PatientProfileWidget::loadPatientInfo() {
    // 请求患者详细信息
    QJsonObject request;
    request["user_id"] = m_patient.id;
    NetworkClient::instance().sendRequest(MSG_GET_PATIENT_INFO, request);
}

void PatientProfileWidget::on_saveButton_clicked() {
    // 收集表单数据
    QString gender = ui->maleRadio->isChecked() ? "男" : "女";
    QDate birthDate = ui->birthDateEdit->date();
    QString address = ui->addressEdit->text().trimmed();
    QString emergencyContact = ui->emergencyContactEdit->text().trimmed();
    QString emergencyPhone = ui->emergencyPhoneEdit->text().trimmed();
    QString allergies = ui->allergiesEdit->toPlainText().trimmed();
    QString medicalHistory = ui->medicalHistoryEdit->toPlainText().trimmed();
    
    // 构建更新请求
    QJsonObject request;
    request["user_id"] = m_patient.id;
    request["gender"] = gender;
    request["birth_date"] = birthDate.toString("yyyy-MM-dd");
    request["address"] = address;
    request["emergency_contact"] = emergencyContact;
    request["emergency_phone"] = emergencyPhone;
    request["allergies"] = allergies;
    request["medical_history"] = medicalHistory;
    
    NetworkClient::instance().sendRequest(MSG_UPDATE_PATIENT_INFO, request);
    
    ui->saveButton->setEnabled(false);
    ui->saveButton->setText("保存中...");
}

void PatientProfileWidget::on_refreshButton_clicked() {
    loadPatientInfo();
}

void PatientProfileWidget::onMessageReceived(MessageType type, const QJsonObject& data) {
    if (type == MSG_GET_PATIENT_INFO_RESPONSE) {
        if (data["success"].toBool()) {
            // 更新患者信息
            m_patient.gender = data["gender"].toString();
            m_patient.birthDate = QDate::fromString(data["birth_date"].toString(), "yyyy-MM-dd");
            m_patient.address = data["address"].toString();
            m_patient.emergencyContact = data["emergency_contact"].toString();
            m_patient.emergencyPhone = data["emergency_phone"].toString();
            m_patient.allergies = data["allergies"].toString();
            m_patient.medicalHistory = data["medical_history"].toString();
            
            // 显示在界面上
            ui->usernameLabel->setText(m_patient.username);
            ui->realNameLabel->setText(m_patient.realName);
            ui->phoneLabel->setText(m_patient.phone);
            
            // 设置性别
            if (m_patient.gender == "男") {
                ui->maleRadio->setChecked(true);
            } else {
                ui->femaleRadio->setChecked(true);
            }
            
            ui->birthDateEdit->setDate(m_patient.birthDate.isValid() ? m_patient.birthDate : QDate::currentDate());
            ui->addressEdit->setText(m_patient.address);
            ui->emergencyContactEdit->setText(m_patient.emergencyContact);
            ui->emergencyPhoneEdit->setText(m_patient.emergencyPhone);
            ui->allergiesEdit->setPlainText(m_patient.allergies);
            ui->medicalHistoryEdit->setPlainText(m_patient.medicalHistory);
        }
    }
    else if (type == MSG_UPDATE_PATIENT_INFO_RESPONSE) {
        ui->saveButton->setEnabled(true);
        ui->saveButton->setText("保存信息");
        
        if (data["success"].toBool()) {
            QMessageBox::information(this, "成功", "患者信息更新成功！");
            loadPatientInfo(); // 重新加载信息
        } else {
            QMessageBox::warning(this, "失败", data["message"].toString());
        }
    }
}

